

function rowspanFun(data, nameList) {
    for (var i = 0; i < nameList.length; i++) {
        var name = nameList[i];
        var startRow = 0;
        var endRow = data.length;
        var mergeNum = 1;
        if (endRow != 1) {
            for (var j = startRow; j < endRow; j++) {
                if (j == endRow - 1) { //判断是否是最后一个元素
                    if (startRow == endRow - 1) {
                        data[j][name + 'Rowspan'] = 1;
                    }
                } else {
                    if (data[startRow][name] == data[j + 1][name]) {
                        data[j + 1][name + 'Rowspan'] = 0;
                        mergeNum = mergeNum + 1;
                        data[startRow][name + 'Rowspan'] = mergeNum;
                    } else {
                        startRow = j + 1;
                        if (mergeNum > 1) {
                            data[startRow][name + 'Rowspan'] = 1;
                        } else {
                            data[j][name + 'Rowspan'] = 1;
                        }
                        mergeNum = 1;
                    }
                }
            }
        } else {
            data[0][name + 'Rowspan'] = 1;
        }
    }
    return data;
}


queryRecord = (tname) => {
    AjaxRequest('/inspection/equipment/account?orgid=' + selectOrgId, callback)
    function callback(result) {

        var data = result.result
        var rowT = []

        for (const key in data[0]) {
            rowT.push(key)
        }

        var data = rowspanFun(data, rowT);


        $('tr').empty().append(rowT.reduce((x, y) => {
            var rName = '';

            switch (y) {
                case 'orgName':
                    rName = '组织名称'
                    break;
                case 'locationName':
                    rName = '所属位置'
                    break;
                case 'pointCode':
                    rName = '点位编号'
                    break;
                case 'equipmentCode':
                    rName = '设备编号'
                    break;
                case 'pointName':
                    rName = '点位名称'
                    break;
                case 'type':
                    rName = '设备类型'
                    break;
                case 'produceDate':
                    rName = '生产日期'
                    break;
                case 'expireDate':
                    rName = '到期时间'
                    break;
                case 'replaceDate':
                    rName = '更换日期'
                    break;
                case 'replacePersonName':
                    rName = '更换人'
                    break;
                case 'replacePersonPhone':
                    rName = '联系方式'
                    break;

                default:
                    break;
            }



            return x + `<th>${rName}</th>`

        }, ''))



        $('tbody').empty().append(data.reduce((x, y) => {


            y.type = checkEqTypeName(y.type)

            return x + `
                    <tr >`+
                rowT.reduce((x1, y1, i) => {

                    if (i > 5 || i == 3) {
                        return x1 + `<td style="text-align:center;">${y[rowT[i]] == null ? '' : y[rowT[i]]}</td>`
                    } else {
                        if (y[rowT[i] + 'Rowspan'] != 0) {
                            return x1 + `<td style="text-align:center;" rowspan=${y[rowT[i] + 'Rowspan']}>${y[rowT[i]] == null ? '' : y[rowT[i]]}</td>`
                        } else {
                            return x1
                        }

                    }


                }, '')
                + `
                    </tr>
                    `
        }, ''))


        // 使用outerHTML属性获取整个table元素的HTML代码（包括<table>标签），然后包装成一个完整的HTML文档，设置charset为urf-8以防止中文乱码
        var html = "<html><head><meta charset='utf-8' /></head><body><table style='width: 100%;' border='1'>" +
            "<caption style='font-size:36px;' rowspan=2>" + tname + "消防器材台账</caption>" + document.getElementsByTagName("table")[0].innerHTML + "</table></body></html>";
        // 实例化一个Blob对象，其构造函数的第一个参数是包含文件内容的数组，第二个参数是包含文件类型属性的对象
        var blob = new Blob([html], { type: "application/vnd.ms-excel" });
        var a = document.getElementById("exportTable");
        // 利用URL.createObjectURL()方法为a元素生成blob URL
        a.href = URL.createObjectURL(blob);
        // 设置文件名
        a.download = tname + "消防器材台账.xls";


    }
}
